package xian.woniuxy.reghos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.redis.utils.RedisUtils;
import com.ruoyi.system.api.RemoteScheduleService;
import com.ruoyi.system.api.domain.Schedule;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import xian.woniuxy.customer.dubboService.RemotePatientService;
import xian.woniuxy.domain.Patient;
import xian.woniuxy.domain.Register;
import xian.woniuxy.reghos.mapper.RegisterMapper;
import xian.woniuxy.reghos.service.GuahaoService;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: song
 * @date: 2022-08-25 09:29:58
 * @desc: TODO22222
 */
@Service
@RequiredArgsConstructor
public class GuahaoServiceImpl implements GuahaoService {

    @DubboReference
    private final RemoteScheduleService remoteScheduleService;
    @DubboReference
    private final RemotePatientService remotePatientService;

    private final RegisterMapper registerMapper;
    @Override
    public String guahao(Long scheduleId, String patientCardId) {
        Patient patient = remotePatientService.queryByCardId(patientCardId);
        if (patient == null) {
            return "noinfo";
        }
        Object o1 = RedisUtils.getCacheObject("guahaoexist:" + patientCardId);
        if (o1 != null) {
            return "exist";
        }
        Schedule schedule = remoteScheduleService.queryById(scheduleId);
        int n = 0;
        Object o = RedisUtils.getCacheObject("guahaonum:" + scheduleId);
        if (o != null) {
            n = Integer.parseInt(o.toString());
        }
        if (n >= schedule.getScheduleNum()) {
            return "no";
        }
        Register register = new Register();
        register.setRegisterDate(new Date());
        register.setDepId(schedule.getDepId());
        register.setDepName(schedule.getDepName());
        register.setPatientId(patient.getPatientId());
        register.setUserId(schedule.getUserId());
        register.setUserName(schedule.getUserName());
        register.setRegisterPrice(BigDecimal.valueOf(5L));
        register.setRegisterState("0");
        register.setPatientName(patient.getPatientName());
        int insert = registerMapper.insert(register);
        RedisUtils.setCacheObject("guahaoexist:"+patientCardId, "true", Duration.ofHours(5));
        RedisUtils.incrAtomicValue("guahaonum:" + scheduleId);
        RedisUtils.setCacheObject("guahaopatient:"+patientCardId, register, Duration.ofHours(5));
        RedisUtils.setCacheObject("guahaoregister:"+register.getRegisterId(), register, Duration.ofHours(5));
        List<Register> registerList = RedisUtils.getCacheList("guahaolist:" + schedule.getUserId());
        if (registerList == null) {
            LinkedList<Register> list = new LinkedList<>();
            list.add(register);
            RedisUtils.setCacheList("guahaolist:" + schedule.getUserId(), list);
            RedisUtils.expire("guahaolist:" + schedule.getUserId(), Duration.ofHours(5));
        } else {
            registerList.add(register);
            RedisUtils.deleteKeys("guahaolist:" + schedule.getUserId());
            RedisUtils.setCacheList("guahaolist:" + schedule.getUserId(), registerList);
            RedisUtils.expire("guahaolist:" + schedule.getUserId(), Duration.ofHours(5));
        }
        return "ok";
    }
}
